/* sdcard_spi.h */

#ifndef __SDCARD_SPI_H__
#define __SDCARD_SPI_H__

#include <stdint.h>
#include <stdbool.h>

#define SDC_BLOCK_SIZE_BYTE     (512u)

typedef struct
{
    void    (*SPI_InitHwFunc)(void);            /* 初始化通信接口 */
    uint8_t (*SPI_SwapByteFunc)(uint8_t txDat); /* 通过SPI传输数据 */
    void    (*SPI_AssertCsFunc)(bool enLow);    /* 选中CS信号 */
} SDC_SPIHwDrv_T;

typedef enum
{
    eSDCCardUnknown    = 0u,
    eSDCCardTypeOfSDv1 = 1u, /* v1 normal.       以字节寻址,不支持CMD8 */
    eSDCCardTypeOfSDv2 = 2u, /* v2 normal.       以字节寻址,支持CMD8*/
    eSDCCardTypeOfSDHC = 3u  /* v2 high capacity.以块寻址,  支持CMD8 */
} SDC_CardType_T;

typedef struct
{
    SDC_CardType_T CardType;    /* 卡类型 */
    uint32_t       CardBlkCnt;  /* 512字节的块数量.在库内部已经对不同类型的卡归一化. */
} SDC_Info_T;

bool SDC_Install(const SDC_SPIHwDrv_T *hwDrv);

bool SDC_InitCard(SDC_Info_T *infoPtr);
bool SDC_WriteBlock(uint32_t blkIdx, uint8_t *txPtr);
bool SDC_ReadBlock(uint32_t blkIdx, uint8_t *rxPtr);
uint32_t SDC_WriteBlocks(uint32_t blkIdx, uint32_t blkCnt, uint8_t *txPtr);
uint32_t SDC_ReadBlocks(uint32_t blkIdx, uint32_t blkCnt, uint8_t *rxPtr);

#endif /* __SDCARD_SPI_H__ */
